package controller

import (
	"RestaurantApp/service"
	"RestaurantApp/utils"
	"github.com/gin-gonic/gin"
	"github.com/wonderivan/logger"
	"strconv"
)

type ShopController struct {
}

func (this *ShopController) Routers(r *gin.Engine) {
	shopGroups := r.Group("/api")
	shopGroups.GET("getShopByCategory/:categoryId", this.getShopByCategoryId) // 通过菜单名目获取商家名
	shopGroups.GET("/shops", this.getShopByPosition)                          // 通过地理位置获取商家
	shopGroups.GET("/search_shops", this.getShopByKeyword)

}

func (this *ShopController) getShopByCategoryId(ctx *gin.Context) {

	id := ctx.Param("categoryId")

	categoryId, _ := strconv.Atoi(id)
	shopService := service.ShopService{}
	shops := shopService.GetShopByCategoryId(categoryId)

	utils.Success(ctx, shops)

}

func (this *ShopController) getShopByPosition(ctx *gin.Context) {
	longitude := ctx.Query("longitude")
	latitude := ctx.Query("latitude")
	flag := false
	if longitude == "" || latitude == "" {
		flag = true // no coordinator offered  没有提供坐标
	}
	shopService := service.ShopService{}
	if flag == true {
		shops := shopService.GetEveryShop()
		logger.Info("没有得到足够的地理坐标信息")
		utils.Success(ctx, shops)
		return
	}
	px, _ := strconv.ParseFloat(longitude, 10)
	py, _ := strconv.ParseFloat(latitude, 10)
	shops := shopService.GetShopByPosition(px, py)
	utils.Success(ctx, shops)
	return

}

func (this *ShopController) getShopByKeyword(ctx *gin.Context) {
	keyword := ctx.Query("keyword")
	shopService := service.ShopService{}
	if keyword == "" {
		logger.Info("搜索关键字为空")
		shops := shopService.GetEveryShop()
		utils.Success(ctx, shops)
		return
	}

	shops := shopService.GetShopByKeyword(keyword)
	utils.Success(ctx, shops)

}
